using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace Dune2013
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        public Map map;
        private static Game1 instance;
        int tiempoEntreHelicopteros = 0;
        Node baseNode, stationNode;

        Recolector recolector = null;
        Protector protector = null;


        public static Game1 getInstance() {
            if (instance == null) {
                instance = new Game1();
            }
            return instance;
        }

        public static void setInstance(Game1 game) {
            instance = game;
        }

        public Game1()
        {
            setInstance(this);
            StaticStuff.initialice(Content);
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";

            map = MapFactory.getInstance().createMap(StaticStuff.CantNodoX, StaticStuff.CantNodoY, StaticStuff.TamNodoX, StaticStuff.TamNodoY,StaticStuff.cantidadMaximaMinasPetroleo, StaticStuff.minCargaPetroleo, StaticStuff.maxCargaPetroleo, StaticStuff.cantidadMaximaObstaculos);
        }

        protected override void Initialize()
        {
            base.Initialize();
        }

        protected override void LoadContent()
        {
            recolector = new Recolector(new IrCosecharBehavior());
            recolector.globalBehavior = new CargadoBehavior();
            protector = new Protector(recolector);
            protector.maxTurnSpeed = StaticStuff.maxTurnSpeedHelicoptero;
            
            ActorManager am = ActorManager.getInstance();
            am.actors.Add(recolector);
            am.actors.Add(protector);
            spriteBatch = new SpriteBatch(GraphicsDevice);
            SpriteFont naftaRestanteFont = Content.Load<SpriteFont>(StaticStuff.naftaRestanteFont);
            Text naftaRestante = new RecolectorStateText(recolector, StaticStuff.distDTxNRAHP, naftaRestanteFont, "Nafta restante: ", StaticStuff.naftaRestantePosition, StaticStuff.naftaRestanteColor);
            TextManager tm = TextManager.getInstance();
            tm.texts.Add(naftaRestante);
        }

        protected override void UnloadContent()
        {

        }

        protected override void Update(GameTime gameTime)
        {
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                this.Exit();
            
            ActorManager.getInstance().update(gameTime.ElapsedGameTime.Milliseconds);
            ActorFactory.generateHelicoptero(recolector, protector, gameTime.ElapsedGameTime.Milliseconds);
            base.Update(gameTime);
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin(); 
            map.render(spriteBatch);
            ActorManager.getInstance().draw(spriteBatch);
            TextManager.getInstance().draw(spriteBatch);
            spriteBatch.End();
            base.Draw(gameTime);
        }

    }
}
